import localCache from '@/utils/cache'
const localStoragePlugin = (store: any) => {
  // 订阅mutation并本地存储
  store.subscribe((mutation: any, state: any) => {
    // state对象含有所有模块下的各种state
    // 任何mutations都会让所有sessionStorage重新设置一遍，vuex设计如此,key值为state中的模块
    for (const [key, val] of Object.entries(state)) {
      if (key === 'user') {
        localCache.setCache(key, val)
      } else {
        window.sessionStorage.setItem(key, JSON.stringify(val))
      }
    }
  })
  // 订阅action并本地存储
  // store.subscribeAction((action, state) => {})
}

export default [localStoragePlugin] // plugins的值需要是数组
